package chapter07;

import java.util.ArrayList;
import java.util.Iterator;

public class Test01_arrayList {
    public static void main(String[] args) {
        //新建arrayList
        ArrayList a1 = new ArrayList();
        //新增元素
        a1.add("张三");
        a1.add(45);
        a1.add(false);
        System.out.println(a1);
        //删除
        a1.remove(1);
        a1.remove(new Integer(45));
        System.out.println(a1);
        //查找
        System.out.println(a1.contains("张三"));
        System.out.println(a1.indexOf("张三"));
        System.out.println(a1.get(0));
        //修改
        a1.set(0,"李四");
        System.out.println(a1);
        //遍历
        //使用迭代器
        //获得迭代器
        Iterator iterator = a1.iterator();
        //进行循环
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //使用foreach循环也可以遍历
        for (Object c: a1) {
            System.out.println(c);
        }
        //使用迭代器可以对元素进行更改 foreach不可以
        Iterator iterator1 = a1.iterator();
        //进行循环
        while(iterator1.hasNext()){
            Object next = iterator1.next();
            if("李四".equals(next)){
                iterator1.remove();
            }
        }
        System.out.println(a1);
    }
}
